package com.JZ.QM.listener;

import com.JZ.QM.common.dao.AdjustmentRecordDao;
import com.JZ.QM.common.entity.*;
import com.JZ.QM.common.utils.Time;
import com.JZ.QM.service.EntrustRecordService;
import com.JZ.QM.service.TradeRecordService;
import com.JZ.QM.service.UserAccountService;
import com.JZ.QM.service.WealthAccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;

/**
 * @author lyp
 * @email lyp@gmail.com
 * @date 2020/7/4 21:17
 */
@Component
@Slf4j
public class RabbitMqListener {

    @Resource
    EntrustRecordService entrustRecordService;

    @Resource
    WealthAccountService wealthAccountService;

    @Resource
    UserAccountService userAccountService;

    @Resource
    TradeRecordService tradeRecordService;

    @Resource
    AdjustmentRecordDao adjustmentRecordDao;

    /**
     * 监听买入委托确认消息
     * @param message
     * @param content
     */
    @RabbitListener(queues = {"ackStockQueue","ackFundQueue"})
    public void purchaseListener(Message message, Long content){
        //委托记录实体
        EntrustRecordEntity entrustRecordEntity = new EntrustRecordEntity();
        //交易记录实体
        TradeRecordEntity tradeRecordEntity =  new TradeRecordEntity();

        //更新状态flag
        boolean updateFlag = true;
        boolean updateTradeRecord = true;

        entrustRecordEntity = entrustRecordService.getById(content);
        entrustRecordEntity.setEntrustStatus(2);
        //变更委托状态
        updateFlag = entrustRecordService.saveOrUpdate(entrustRecordEntity);

        tradeRecordEntity = tradeRecordService.getById(entrustRecordEntity.getTradeId());
        tradeRecordEntity.setTradeCategory(2);
        //变更交易记录状态
        updateTradeRecord = tradeRecordService.saveOrUpdate(tradeRecordEntity);

        if(updateFlag && updateTradeRecord){
            log.info("委托流水号:"+entrustRecordEntity.getLogicId()+"===>"+"委托成功");
        }else {
            log.info("委托流水号:"+entrustRecordEntity.getLogicId()+"===>"+"委托失败");
        }
    }


    /**
     * 监听赎回委托确认消息
     * @param message
     * @param content
     */
    @RabbitListener(queues = {"ackStockRedemptionQueue","ackFundRedemptionQueue"})
    public void redemptionListener(Message message,Long content) throws ParseException {
        //委托记录实体
        EntrustRecordEntity entrustRecordEntity = new EntrustRecordEntity();
        //交易记录实体
        TradeRecordEntity tradeRecordEntity =  new TradeRecordEntity();
        //更新状态flag
        boolean updateFlag = true;
        boolean updateTradeRecord = true;

        entrustRecordEntity = entrustRecordService.getById(content);
        entrustRecordEntity.setEntrustStatus(2);
        //变更委托状态
        updateFlag = entrustRecordService.saveOrUpdate(entrustRecordEntity);

        tradeRecordEntity = tradeRecordService.getById(entrustRecordEntity.getTradeId());
        tradeRecordEntity.setTradeCategory(4);

        //变更交易记录状态
        updateTradeRecord = tradeRecordService.saveOrUpdate(tradeRecordEntity);

        if(updateFlag && updateTradeRecord){
            log.info("委托流水号:"+entrustRecordEntity.getLogicId()+"===>"+"委托成功");
        }else {
            log.info("委托流水号:"+entrustRecordEntity.getLogicId()+"===>"+"委托失败");
        }

        //根据财富账户查询修改时间
        WealthAccountEntity wealthAccountEntity = wealthAccountService.getById(entrustRecordEntity.getWealthAccountId());

        //获取建仓时间
        Date update = wealthAccountEntity.getGmtCreate();
        //赎回费率
        BigDecimal ratio = BigDecimal.valueOf(0);

        //计算持有时长
        Date now = new Date();
        int days = Time.daysBetween(now, update);

        if(days <= 7){
            ratio = BigDecimal.valueOf(0.015);
        }else if( 7 < days && days <= 30){
            ratio = BigDecimal.valueOf(0.01);
        }else {
            ratio = BigDecimal.valueOf(0.005);
        }
        //根据财富账号拿到平台账户
        UserAccountEntity userAccountEntity = userAccountService.getById(wealthAccountEntity.getAccountId());

        if(wealthAccountEntity.getWealthAccountAmoount().subtract(entrustRecordEntity.getEntrustAmount()).compareTo(BigDecimal.valueOf(0)) == 0){
            wealthAccountEntity.setIsUsed(0);
        }
        //财富账号扣款
        wealthAccountEntity.setWealthAccountAmoount(wealthAccountEntity.getWealthAccountAmoount().subtract(entrustRecordEntity.getEntrustAmount()));

        boolean deductionFlag = wealthAccountService.updateById(wealthAccountEntity);
        //赎回费用
        BigDecimal redemptionCost = entrustRecordEntity.getEntrustAmount().multiply(ratio);
        //平台账户转账
        userAccountEntity.setAccountAmoount(userAccountEntity.getAccountAmoount().add(entrustRecordEntity.getEntrustAmount().subtract(redemptionCost)));
        boolean transferFlag = userAccountService.updateById(userAccountEntity);
        if(deductionFlag && transferFlag){
            log.info("赎回成功");
        }else {
            log.info("赎回失败");
        }
    }


    /**
     * 监听调仓委托确认消息
     * @param message
     * @param content
     */
    @RabbitListener(queues = {"ackFundAdjustmentQueue","ackStockAdjustmentQueue"})
    public void adjustListener(Message message,Long content){
        //委托记录实体
        EntrustRecordEntity entrustRecordEntity = new EntrustRecordEntity();
        //调仓记录实体
        AdjustmentRecord adjustmentRecord = new AdjustmentRecord();

        //更新状态flag
        boolean updateFlag = true;
        int updateAdjustRecord = 1;

        entrustRecordEntity = entrustRecordService.getById(content);
        //卖出类型委托
        if(entrustRecordEntity.getEntrustType() == 1){
            entrustRecordEntity.setEntrustStatus(2);
            //买入类型委托
        }else {
            entrustRecordEntity.setEntrustStatus(2);
        }
        //变更委托状态
        updateFlag = entrustRecordService.saveOrUpdate(entrustRecordEntity);

        adjustmentRecord = adjustmentRecordDao.selectById(entrustRecordEntity.getRecordId());
        adjustmentRecord.setAdjustStatus(2);
        //变更调仓记录状态
        updateAdjustRecord = adjustmentRecordDao.updateById(adjustmentRecord);

        if( ( updateFlag == true ) && ( updateAdjustRecord == 1) ){
            log.info("委托流水号:"+entrustRecordEntity.getLogicId()+"===>"+"委托成功");
        }else {
            log.info("委托流水号:"+entrustRecordEntity.getLogicId()+"===>"+"委托失败");
        }
    }
}
